if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_GetAllOrderByUserID]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_GetAllOrderByUserID]
GO
CREATE PROC sp_GetAllOrderByUserID
	@userID int
AS
BEGIN
	SELECT A.ID, A.AccountID, A.Date, A.Description, A.Status, A.ApprovedByUser, A.ApprovedDate, A.RejectReason, A.TotalAmount, A.LastUpdate, A.Discount, 
		(B.FirstName + ' ' + B.LastName) as CusName, (C.FirstName + ' ' + C.LastName) as dealerName
		FROM OrderRequest As A INNER JOIN AccountInformation As B ON A.AccountID=B.AccountID
			Left Join AccountInformation As C ON A.ApprovedByUser=C.AccountID
		WHERE A.AccountID = @userID
		ORDER BY A.Date DESC
END